⇠ RfficeHours Home || Tutorial 2 ⇢
| Learning Outcomes | |
|---|---|
| 1 | You will be able to open RStudio and know where to find basic functions |
| 2 | You will be able to create and organise a new project |
| 3 | You will be able to load your data into R and view it |
| 4 | You will know the purpose and of code annotation and commenting |
Before you can use R and RStudio, it’s important to understand what you’re looking at and where to find things.
R is the program and programming language that allows you to input commands and get the computer to do things. In order to interact with R, some people use a simple R interface, some people use the command line, and some people use RStudio.
RStudio is a GUI (Graphical User Interface) that allows you to interact with R and keep everything organised.
R interface
Command line
RStudio
We’ll be using RStudio because it is the easiest to use, with some point-and-click commands, but still with the full functionality and power of R. R runs in the background when you run RStudio, but RStudio takes care of that on its own so all you need to do is open RStudio.
The RStudio interface has (up to) four panes that you can rearrange and customise to suit your needs. Here is the default configuration:
When you open RStudio for the first time, there may only be three panes.
The console is your direct line of communication with R. It operates a bit like a chat window (if you’re familiar with that) because you can type things into the console, hit ENTER, and R will do something (and sometimes, depending on what you type, it’ll respond). You know the console is listening and ready to accept a new command if you see a > on the left edge of the window on the lowest line of text.
The environment is a set of three tabs that effectively lets us see into “R’s brain” (thanks Danielle!). This window lets you see what variables you’ve created, what datasets are loaded in, what packages and libraries are loaded, among many other things. Right now, it’s empty.
If we click on Global Environment, we can see what packages have automatically be loaded into this R session. Once you load other packages in, you will see them here too.
The pane in the lower right has five tabs by default.
Zoom to pop the graph out into a separate window and resize it.Export to save the plot as a PNG, PDF, EPS, etc.Scripts will appear in the fourth pane (top left by default).
Scripts are simply text files, they are not R and they don’t do anything unless you perform specific actions on them. They’re a bit like instruction manuals, but R can only read them if you manually send the instructions to the console (more on this later).
A script is a way to save your work so you only need to write the code once. Once you’ve written a script once, you can execute it as many times as you like, but you won’t need to write it again. You can copy and paste other people’s code into your script and tweak it to fit your needs. You can debug your code without having to type it in new each time. You can share your code with others, and you can leave comments to yourself in your code so that you can leave it sit for a while and then remember what you were doing when you come back to it.
On all platforms: the green + symbol in the top left corner will let you create a new script.
On a Mac: command+shift+N
On a PC:
The symbol # hides the text that follows on that line from R, that is, it “comments it out”. This lets you write comments to yourself and to anyone else who might read your code. You’ll want to do this so you can remember what you were doing (trust me, you will not remember) and so other people can replicate what you did (even if it’s just to help you debug your code later).
If you type print("Hello World!") (with a nice comment) and then hit ENTER in your console, you will see something like this:
> print("Hello World!") # this line will produce the text between the quotes
[1] "Hello World!"
If you type print("Hello World!") and hit ENTER in a script, you will see something like this:
print("Hello World!")
(Note that nothing happens. There is no output. A script is just a text file.)
Open a new script and type print("Hello World!") into it. How do you get R to execute your code? There are a couple ways:
Run button on the top right portion of the script pane.
Command+EnterCommand+EnterCommand+Shift+EnterWhen you run a script, the text in the script is sent, line by line, to the console. Once in the console, R executes the code. You can watch the code progress in the console. If there is any text or numerical output, it will appear in the console. If there is a graphical output, it will appear in the Plots tab of the lower right pane (Files etc).
In the script, enter the following code:
print("Hello world!")
3 + 4
7^2Run the entire script.
Your console should end up looking like this:
> print("Hello world!")
[1] "Hello world!"
> 3 + 4
[1] 7
> 7^2
[1] 49
>
What does [1] mean?
The number in square brackets to the left of the output indicates the number of values that have been printed by index number. That is, if there are multiple outputs on one line, [1] will appear. If the seventh output wraps around and appears on the second line, both [1] and [7] will appear. This is useful when trying to make sense of long lists of numbers, for instance.
Best practices for file organisation
Reading and writing files
Opening and creating scripts
Opening datasets
Rmarkdown and literate programming
Why do we annotate code?
What is literate programming?